package com.huishen.edrivenew.widget;

import com.huishen.edrivenew.AppContext;
import com.huishen.edrivenew.R;
import com.huishen.edrivenew.bean.DateBean;
import com.huishen.edrivenew.ui.MainActivity;
import com.huishen.edrivenew.util.TLog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 日历页面
 * @author wu
 *
 */

public class CalendarGridAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater mInflater;

    public CalendarGridAdapter(Context context) {
        super();
        this.context = context;
        mInflater = LayoutInflater.from(context);
        
        AppContext.getInstance().caAdapter = this;
    }



    private void setDate(ViewHolder holder, int position, View convertView) {
        
        DateBean bean = AppContext.getInstance().days[position];
        
        holder.text.setText("" + bean.getDayNumber());


        if(bean.canClick) {
            holder.text.setTextColor(AppContext.resources().getColor(R.color.tv_gray_2));
            holder.bg.setImageResource(R.drawable.selector_ca);
            // 拦截item点击事件
//            convertView.setClickable(false);
//            convertView.setEnabled(true);
//            convertView.setSelected(true);
            // 设置点击事件
            setClick(holder.bg, bean);
        }

        if(bean.isToday) {
            holder.bottomTv.setText("当日");
            holder.bg.setImageResource(R.drawable.selector_ca_wri);
//            convertView.setBackgroundResource(R.drawable.selector_ca_wri);
        }

        if(position < 7) {
            holder.line.setVisibility(View.INVISIBLE);
        } else {
            holder.line.setVisibility(View.VISIBLE);
        }
        switch (bean.state) {
            case -1:
                
                break;
            case 0:
                            
                break;
            case 1: 
                holder.bg.setImageResource(R.drawable.selector_ca_green);
//                convertView.setBackgroundResource(R.drawable.ca_green);
                break;
            case 2:
                holder.bg.setImageResource(R.drawable.selector_ca_red);
//                convertView.setBackgroundResource(R.drawable.ca_red);
                break;

            default:
                break;
        }
    }


    public void setClick(ImageButton btn, final DateBean bean) {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(AppContext.getInstance().mainActivity != null) {
                    AppContext.getInstance().mainActivity.onCaClick(bean);
                }
            }
        });
    }


    @Override
    public int getCount() {
        return 14;
    }

    @Override
    public Object getItem(int position) {
        return AppContext.getInstance().days[position];
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.calendar_day, null);
            holder = new ViewHolder();

            holder.text = (TextView) convertView.findViewById(R.id.day);
            holder.bottomTv = (TextView) convertView.findViewById(R.id.day_bottomtv);
            holder.line = convertView.findViewById(R.id.ca_line);
            holder.bg = (ImageButton) convertView.findViewById(R.id.ca_bg);
            
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        setDate(holder, position, convertView);
        return convertView;
    }


    /** 存放控件 */
    public final class ViewHolder {
        public TextView text;
        public TextView bottomTv;
        public ImageButton bg;
        
        public View line;
    }

}
